<!DOCTYPE html>
<style>
  body {
    width: 1000px;
    height: 1000px;
    /* Overflow hidden so that the size of the scrollbar is not added to
    the innerHeight/Width properties. */
    overflow: hidden;
  }
</style>
<script src="../../resources/js-test.js"></script>

<script type="text/javascript">
  var testScrolls = [];
  var currentTest = -1;

  setPrintTestResultsLazily();
  jsTestIsAsync = true;

  description("Test that when the scrollLayoutViewport setting is on, the scroll " +
    "from window.scrollTo is applied to the layout viewport. Note that this test is " +
    "pertaining to crbug.com/489206, where we want all APIs to reflect the layout viewport.");

  function finishTest() {
    var testCase = testScrolls[currentTest];
    if(window.scrollX == testCase.expectedX && window.scrollY == testCase.expectedY) {
      testPassed("Scroll destination reached.");
      startNextTestCase();
    } else {
      testFailed("Scroll destination not reached.");
      startNextTestCase();
    }
  }

  function startNextTestCase() {
    currentTest++;
    if (currentTest >= testScrolls.length) {
      finishJSTest();
      return;
    }

    var testCase = testScrolls[currentTest];
    window.scrollTo(testCase.x, testCase.y);
    window.requestAnimationFrame(finishTest);
  }

  window.onload = function () {
    if (!window.internals) {
      testFailed('This test requires window.internals');
      finishJSTest();
      return;
    }

    // The height/width of the layout viewport is innerHeight/Width of the window
    // when no pinch-zoom is applied.
    var layoutInnerHeight = window.innerHeight;
    var layoutInnerWidth = window.innerWidth;
    // The height/width of the visual viewport is innerHeight/Width of the window
    // when pinch-zoom is applied.
    internals.setPageScaleFactor(2.0);
    var visualInnerHeight = window.innerHeight;
    var visualInnerWidth = window.innerWidth;

    // The maximum scroll offsets when the visual viewport is scrolled.
    var maxScrollHeightScrollVisual = document.scrollingElement.scrollHeight - visualInnerHeight;
    var maxScrollWidthScrollVisual = document.scrollingElement.scrollWidth - visualInnerWidth;
    // The maximum scroll offsets when the layout viewport is scrolled.
    var maxScrollHeightScrollLayout = document.scrollingElement.scrollHeight - layoutInnerHeight;
    var maxScrollWidthScrollLayout = document.scrollingElement.scrollWidth - layoutInnerWidth;

    // First scrollTo is called with the scrollLayoutViewport setting off, in which
    // case, window.scrollY should return the position of the visual viewport. Then
    // the setting is turned on, and this time, window.scrollY should return the
    // position of the layoutViewport.
    testScrolls = [
      {x: 2000, y: 2000, expectedX: maxScrollWidthScrollVisual, expectedY:maxScrollHeightScrollVisual, scrollLayoutViewport:false},
      {x: 2000, y: 2000, expectedX: maxScrollWidthScrollLayout, expectedY:maxScrollHeightScrollLayout, scrollLayoutViewport:true},
    ];
    startNextTestCase();
  }
</script>
